package com.xdwanj.smartcity.data.repository;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.lifecycle.MutableLiveData;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.xdwanj.smartcity.App;
import com.xdwanj.smartcity.data.config.Configs;
import com.xdwanj.smartcity.data.repository.impl.UploadRepository;
import com.xdwanj.smartcity.data.request.UploadRequest;
import com.xdwanj.smartcity.util.Constants;

import org.junit.Before;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

public class IUploadRepositoryTest {

    private Context context;
    private SharedPreferences sp;
    private RequestQueue queue;
    private Gson gson;
    private UploadRepository repository;

    @Before
    public void setUp() throws Exception {
        gson = App.gson;
        queue = App.queue;
        sp = App.sp;
        context = App.context;
        repository = UploadRepository.getInstance();
    }

    @Test
    public void upload() throws InterruptedException {
        MutableLiveData<String> data = new MutableLiveData<>();
        UploadRequest request = new UploadRequest(
                Configs.BASE_URL + "/prod-api/common/upload", null, Configs.LOCAL_ENCODE,
                new UploadRequest.FileEntity(
                        "img",
                        "Singleton.jpg",
                        new File("/data/user/0/com.xdwanj.smartcity/files/Singleton.jpg"),
                        "application/octet-stream"
                ),
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        data.setValue(response);
                        System.out.println("response = " + response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("error = " + error);
                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
                map.put(
                        Constants.AUTHORIZATION,
                        "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjdkYzU1OGE2LThlM2QtNGIyZi05ZDEwLTAwZTkwNzJkYjFhMyJ9.QpKsll3v-k_fyZte03mPfkgHPb2VxfqgIZ1yI-9sXwT1lcR4cUOne85wYw6fnC4_qu-wWv4Pj0fwuA3E6U2geg"
                );
                return map;
            }
        };
        queue.add(request.setShouldCache(false));
        Thread.sleep(500);
        System.out.println("data.getValue() = " + data.getValue());
    }

    @Test
    public void test() throws InterruptedException {
        File file = new File("/data/user/0/com.xdwanj.smartcity/files/Singleton.jpg");
        MutableLiveData<String> data = new MutableLiveData<>();
        StringRequest request = new StringRequest(Request.Method.POST, Configs.BASE_URL + "/prod-api/common/upload",
                response -> {
                    System.out.println("response = " + response);
                    data.setValue(response);
                },
                error -> System.out.println("error = " + error)
        ) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                try (
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ) {
                    byte[] bytes = new byte[1024];
                    while (bis.read(bytes) != -1) {
                        bos.write(bytes);
                    }
                    bos.flush();
                    return bos.toByteArray();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return super.getBody();
            }

            @Override
            public String getBodyContentType() {
                return "multipart/form-data";
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
                map.put(
                        Constants.AUTHORIZATION,
                        "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjdkYzU1OGE2LThlM2QtNGIyZi05ZDEwLTAwZTkwNzJkYjFhMyJ9.QpKsll3v-k_fyZte03mPfkgHPb2VxfqgIZ1yI-9sXwT1lcR4cUOne85wYw6fnC4_qu-wWv4Pj0fwuA3E6U2geg"
                );
                return map;
            }
        };
        Thread.sleep(1000);
        System.out.println("data.getValue() = " + data.getValue());
    }
}